﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kpmg.CompanyInfo;
using KpmgRepeaterNew = Kpmg.Web.KpmgRepeater;
using MySearch;

public partial class User_EditProfile : System.Web.UI.Page
{
    private const string CODE_SEPERATOR = "-";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["OBJECT_ID"] == null || Request["OBJECT_ID"] == "")
                Response.Redirect("~/");

            this.hfObjectID.Value = Request["OBJECT_ID"].ToString();
        }

        Prepare();
    }

    protected void Prepare()
    {
        string iid = Utilities.getLoginIID();
        string objectID = this.hfObjectID.Value;
        Profile profile = ProfileOM.GetProfile(objectID);

        PrepareOpp(iid, objectID);
        PrepareChargeable(iid, objectID);
        PrepareTeamMember();
    }

    protected void PrepareOpp(string iid, string objectID)
    {
        //var profile = Utilities.convertToHashTable(Ticketing.getProfileList(iid, objectID));
        //var oppInfo = Utilities.getOppInfo(objectID) as DataTable;
        //this.hfObjectID.Value = objectID;
        //this.hfProfileID.Value = profile["profile_id"].ToString();
        //if (oppInfo == null || oppInfo.Rows.Count == 0)
        //    Response.Redirect("~/Default.aspx");
        //DataRow row = oppInfo.Rows[0];
        //this.OppLabel.Text = row["OBJECT_ID"] + CODE_SEPERATOR + row["DESCRIPTION"];
        //this.ClientCodeLabel.Text = row["client_code"].ToString();
        //this.ClientDescLabel.Text = row["client_desc"].ToString();
        //this.OppStatusLabel.Text = row["STATUS_DESC"].ToString();
        //this.OPPServiceTypeLabel.Text = row["SERVICE_TYPE"] + CODE_SEPERATOR + row["SERVICE_TYPE_DESC"];
        //this.OppPartnerLabel.Text = StaffDAO.GetObject(row["PARTNER"].ToString()).DisplayName;
        //this.OppManagerLabel.Text = StaffDAO.GetObject(row["MANAGER"].ToString()).DisplayName;

        Profile profile = ProfileOM.GetProfile(objectID);
        OppLabel.Text = profile.ProfileCode + "-" + profile.Description;
        ClientCodeLabel.Text = profile.Fields["Client_Code"].Value;
        ClientDescLabel.Text = profile.Fields["Client_Name"].Value;
        OppStatusLabel.Text = profile.Fields["Profile_Status"].Value;
        OPPServiceTypeLabel.Text = string.Empty;

        IList<CommonMember> lst = profile.Members.Values.ToList();
        CommonMember cmPar = lst.Where(x => x.Role == CommonMemberRole.Partner).FirstOrDefault();
        if (cmPar != null)
        {
            OppPartnerLabel.Text = cmPar.Staff.DisplayName;
        }
        else
        {
            OppPartnerLabel.Text = string.Empty;
        }
        CommonMember cmMan = lst.Where(x => x.Role == CommonMemberRole.Manager).FirstOrDefault();
        if (cmMan != null)
        {
            OppManagerLabel.Text = cmMan.Staff.DisplayName;
        }
        else
        {
            OppManagerLabel.Text = string.Empty;
        }
    }

    protected void PrepareChargeable(string iid, string objectID)
    {
        //HashSet<KeyValuePair<Int64, Int64>> dictProfileChargeable = null;
        //IList<Profile> lstProfile = DataUtil.ListProfile(iid, out dictProfileChargeable);
        //Profile profile = lstProfile.Where(x => x.TargetCode.Equals(objectID)).FirstOrDefault();
        //IList<ProfileChargeable> lstChargeable = new List<ProfileChargeable>();
        //foreach (Int64 charableID in dictProfileChargeable.Where(x => x.Key == profile.ProfileID).Select(y => y.Value))
        //    lstChargeable.Add(profile.Chargeables.Where(x => x.ChargeableID == charableID).FirstOrDefault());

        //CreateRepeaterForChargeable0(profile.ProfileID, lstChargeable);
        //CreateRepeaterForChargeable(this.rptChargeable, this.hdSortBy.Value, profile.ProfileID, lstChargeable);
        //this.rptChargeable.ItemCommand += rptChargeable_ItemCommand;


    }

    protected void PrepareTeamMember()
    {
        //DataTable profileTeam = Ticketing.getProfileTeam(this.hfObjectID.Value, Ticketing.CONST_PROFILE_TYPE_OPP);

        //CreateRepeaterForTeamMember(this.rptTeamMember, this.hdSortBy2.Value, profileTeam);
        //this.rptTeamMember.ItemCommand += rptTeamMember_ItemCommand;

        //CreateRepeaterForTeamMember0(profileTeam);
    }

    private void CreateRepeaterForChargeable0(Int64 profileID, IList<Chargeable> lstChargeable)
    {
        IList<Kpmg.Web.KpmgRepeaterColumn> columns = new List<Kpmg.Web.KpmgRepeaterColumn>();
        IList<Kpmg.Web.KpmgRepeaterRow> rows = new List<Kpmg.Web.KpmgRepeaterRow>();

        #region Define column definition
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "ChargeableType", Title = "Chargeable type", Width = 100 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "ChargeableTo", Title = "Chargeable to", Width = 100, Sortable = Kpmg.Web.Sortable.Sort1_Descending });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "Action", Title = "Action", Width = 100, ColumnType = Kpmg.Web.KpmgRepeaterDataType.LinkButton });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "Status", Title = "Status", Width = 100 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "CreateTicket", Title = "Create ticket", Width = 100, ColumnType = Kpmg.Web.KpmgRepeaterDataType.Button });
        #endregion

        //#region Get data
        //Page pageCurrent = HttpContext.Current.Handler as Page;

        //lstChargeable = lstChargeable.Where(x => x != null && x.IsActive == true).Distinct().ToList();
        //IList<IDictionary<string, KpmgRepeaterColumnData>> lstDataRows = new List<IDictionary<string, KpmgRepeaterColumnData>>();
        //IList<EngagementInfo> lstEngagementInfo = EngagementInfoDAO.ListEngagementInfoes(lstChargeable.Select(x => x.TargetCode).Distinct().ToList());
        //StringBuilder sbValue = new StringBuilder();

        //foreach (var obj in lstChargeable)
        //{
        //    Kpmg.Web.KpmgRepeaterRow row = new Kpmg.Web.KpmgRepeaterRow();
        //    row.Items = new Dictionary<string, Kpmg.Web.KpmgRepeaterCell>();

        //    var type = obj.ChargeableType.ToString();
        //    row.Items.Add("ChargeableType", new Kpmg.Web.KpmgRepeaterCell() { Value = type, SortValue = type });

        //    if (obj.ChargeableType == ProfileChargeableType.ENGAGEMENT)
        //    {
        //        sbValue.Clear();
        //        sbValue.Append(DisplayUtil.WithSpan(obj.TargetCode, FontColor.GreenBold));
        //        EngagementInfo engInfo = lstEngagementInfo.FirstOrDefault(x => x.EngCode == obj.TargetCode);
        //        if (engInfo != null)
        //        {
        //            sbValue.Append(DisplayUtil.WithSpan(engInfo.EngDesc, FontColor.Grey, 1));
        //            sbValue.Append(DisplayUtil.WithSpan("Engagement status: " + engInfo.JobStatus, FontColor.Grey, 2));
        //        }
        //        row.Items.Add("ChargeableTo", new Kpmg.Web.KpmgRepeaterCell() { Value = sbValue.ToString(), SortValue = sbValue.ToString() });

        //        sbValue.Clear();
        //        //if (obj.IsInherited)
        //        //    sbValue.Append("<a href='#'>Edit</a>");
        //        //else
        //        //    sbValue.Append("<a href='#'>Manage engagement team</a>");
        //        if (obj.IsInherited)
        //            sbValue.Append("Edit");
        //        else
        //            sbValue.Append("Manage engagement team");

        //        Kpmg.Web.KpmgRepeaterCommand command1 = new Kpmg.Web.KpmgRepeaterCommand("Edit");
        //        command1.CommandArgument.Add("OBJECTID", obj.ChargeableID.ToString());
        //        row.Items.Add("Action", new Kpmg.Web.KpmgRepeaterCell() { Value = sbValue.ToString(), SortValue = sbValue.ToString(), Command = command1 });

        //        sbValue.Clear();
        //        string strStatus = String.Empty;

        //        if (obj.IsInherited == true)
        //        {
        //            strStatus += "Team inherited from profile";
        //        }
        //        else
        //        {
        //            strStatus = "<img src='" + pageCurrent.ResolveUrl("~/images/pencil.gif") + "'></img>";
        //            strStatus += "Team manually managed";
        //        }
        //        row.Items.Add("Status", new Kpmg.Web.KpmgRepeaterCell() { Value = strStatus, SortValue = "" });
        //    }
        //    else
        //    {
        //        row.Items.Add("ChargeableTo", new Kpmg.Web.KpmgRepeaterCell() { Value = sbValue.ToString(), SortValue = sbValue.ToString() });

        //        row.Items.Add("Action", new Kpmg.Web.KpmgRepeaterCell() { Value = sbValue.ToString(), SortValue = sbValue.ToString() });

        //        string strStatus = "Team inherited from profile";
        //        string actionImg = "<img src='~/images/pencil.gif'>" + strStatus + "</img>";
        //        row.Items.Add("Status", new Kpmg.Web.KpmgRepeaterCell() { Value = actionImg, SortValue = "" });
        //    }

        //    sbValue.Clear();
        //    //string actionUrl = "RemotePost.aspx?RequestUrl=CreateTicket.aspx&ProfileID=" + profileID.ToString() + "&ChargeableID=" + obj.ChargeableID;
        //    //string actionCommand = "Create new ticket";
        //    //string strAction = @"&nbsp;<a href='" + actionUrl + "' class='FormButton2013'><span class=''>" + actionCommand + "</span></a>";
        //    //dicDataRow.Add("CreateTicket", new KpmgRepeaterColumnData(strAction, ""));
        //    Kpmg.Web.KpmgRepeaterCommand command2 = new Kpmg.Web.KpmgRepeaterCommand("CreateNewTicket");
        //    command2.CommandArgument.Add("OBJECTID", obj.ChargeableID.ToString());
        //    row.Items.Add("CreateTicket", new Kpmg.Web.KpmgRepeaterCell() { Value = "Create new ticket", SortValue = "", Command = command2});
        //    rows.Add(row);
        //}
        //#endregion

        //KpmgRepeaterNew krpt = new KpmgRepeaterNew("Chargeable", 600, this.hdSortBy, columns, rows);
        //krpt.CustomItemCommand += krpt1_CustomItemCommand;
        //krpt.Create(this.rptChargeable);
    }

    void krpt1_CustomItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if(e.CommandName == "Edit")
        {
            //IDictionary<string, string> dict = DataUtil.JsonToDictionary(e.CommandArgument as string);
            //if (dict != null && dict.Count > 0)
            //{
            //    this.TeamMaintenanceModalPopup.Show(dict["OBJECTID"], false);
            //}
        }
        else if (e.CommandName == "CreateNewTicket")
        {

        }

    }

    private void CreateRepeaterForTeamMember0(DataTable source)
    {
        IList<Kpmg.Web.KpmgRepeaterColumn> columns = new List<Kpmg.Web.KpmgRepeaterColumn>();
        IList<Kpmg.Web.KpmgRepeaterRow> rows = new List<Kpmg.Web.KpmgRepeaterRow>();

        #region Define column definition
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "StaffNo", Title = "Staff no", Width = 30 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "StaffName", Title = "Staff name", Width = 100 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "ProfitCentre", Title = "Profit centre", Width = 180 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "Title", Title = "Title", Width = 120 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "Remarks", Title = "Remarks", Width = 120 });
        columns.Add(new Kpmg.Web.KpmgRepeaterColumn() { Code = "Action", Title = "Action", Width = 30, ColumnType = Kpmg.Web.KpmgRepeaterDataType.Button });
        #endregion

        #region Get data
        foreach (DataRow row in source.Rows)
        {
            Kpmg.Web.KpmgRepeaterRow krow = new Kpmg.Web.KpmgRepeaterRow();
            krow.Items = new Dictionary<string, Kpmg.Web.KpmgRepeaterCell>();

            krow.Items.Add("StaffNo", new Kpmg.Web.KpmgRepeaterCell() { Value = row["staff_no"].ToString(), SortValue = row["staff_no"].ToString() });
            krow.Items.Add("StaffName", new Kpmg.Web.KpmgRepeaterCell() { Value = row["display_name"].ToString(), SortValue = row["display_name"].ToString() });
            string deptInfo = "";//row["dept_no"].ToString() + " - " + Utilities.getCostCentre(row["dept_no"].ToString())["cost_centre_desc"];
            krow.Items.Add("ProfitCentre", new Kpmg.Web.KpmgRepeaterCell() { Value = deptInfo, SortValue = deptInfo });
            krow.Items.Add("Title", new Kpmg.Web.KpmgRepeaterCell() { Value = row["title"].ToString(), SortValue = row["title"].ToString() });
            krow.Items.Add("Remarks", new Kpmg.Web.KpmgRepeaterCell() { Value = row["remarks"].ToString(), SortValue = row["remarks"].ToString() });

            string strAction = "Remove";
            Kpmg.Web.KpmgRepeaterCommand command1 = new Kpmg.Web.KpmgRepeaterCommand("Remove");
            krow.Items.Add("Action", new Kpmg.Web.KpmgRepeaterCell() { Value = strAction, SortValue = "", Command = command1 });
            rows.Add(krow);
        }
        #endregion

        KpmgRepeaterNew krpt = new KpmgRepeaterNew("TeamMember", 600, this.hdSortBy2, columns, rows);
        krpt.CustomItemCommand += krpt2_CustomItemCommand;
        krpt.Create(this.rptTeamMember);
    }

    void krpt2_CustomItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
    }
}